function Game()
{
// * * * * * * * * * * * * * * * * * * * *//
//			VARIABLES	  //
// * * * * * * * * * * * * * * * * * * * *//
this.planets=[];
this.stars=[];
this.colors=["F30","660","6CF","3F3","CF0","009","630"];
this.loop=false;


this.canvas;
this.context;
//



requestAnimationFrame= window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

// * * * * * * * * * * * * * * * * * * * *//
//			FUNCTIONS	  				  //
// * * * * * * * * * * * * * * * * * * * *//

	this.start = function( canvas )
	{
		this.canvas=canvas;
		this.context=canvas.getContext("2d");
		var that=this;
		canvas.addEventListener("click",function(){

			if( !that.loop )
			{
                            requestAnimationFrame( that.gameLoop.bind(that) );
			}
                        else that.action();
			that.loop = !that.loop;

		});
		
		for(i=0;i<1;++i)
                       this.addPlanet();
               // for(i=0;i<10;++i)this.addStar();
                //a=setInterval( this.addStar.bind(this),1000 );
                this.addStar();
		
	}
        
//performing when clicking on the game
	this.action = function()
	{
//check the collision with any stars(same color) for every planet's stream(line)
            for( i in this.planets )
            {
                var planet = this.planets[i];
                var px = (7+i*6)*Math.sin( planet.angle );
                var py = (7+i*6)*Math.cos( planet.angle );
                for( k in this.stars )
                {
                    var star = this.stars[k];
                    var sx = star.posX - canvas.width/2;
                    var sy = -(star.posY - canvas.height/2);
                    var closestStarDistance = 0 ;
                    
                    //console.log("------------------------------------------------------------");
                    var star2sun = Math.sqrt(sx*sx + sy*sy) ;
                    var star2planet = Math.sqrt( (sx-px)*(sx-px)+(sy-py)*sy-py ) ;
                    //console.log( Math.sin(planet.angle) + " ? " + sx/star2sun );
                    //console.log( Math.cos(planet.angle) + " ? " + sy/star2sun );
                    //odleglosc gwiazda-promien
                    //console.log( "----->"+planet.angle%(Math.PI/2) );
                    //console.log( Math.sin(planet.angle%(Math.PI/2)) );
                    console.log( "---------------" );
                    //console.log( Math.asin(Math.sin(planet.angle%(Math.PI/2))) );
                    console.log( Math.asin(Math.abs(sx/star2sun)%(Math.PI/2))*180 );
                    //console.log( Math.abs(Math.sin(planet.angle))+"-"+sx/star2sun+"="+Math.abs( Math.sin(planet.angle)-sx/star2sun) );
                   // console.log( Math.abs( (Math.abs(Math.sin(planet.angle))-sx/star2sun)*star2planet ) );
                    //porownuj sinusy planety i gwiazdy(do slonca). teraz moze to mozna jakos ogarnac na podstawie odleglosci czy cos(gwiazdy ofc...)
					
					
                    /**var lineLength = Math.sqrt( ( canvas.width/2*canvas.width/2 ) + ( canvas.height/2*canvas.height/2 ) );
                    var lineEndX = lineLength * Math.sin( planet.angle ) + this.canvas.width/2;
                    var lineEndY = lineLength * Math.cos( planet.angle ) + this.canvas.height/2;
                    var star2sun = Math.sqrt( (this.canvas.width/2-sx)*(this.canvas.width/2-sx) + (this.canvas.height/2-sy)*(this.canvas.height/2-sy) ) ;
                    var star2edge = Math.sqrt( (lineEndX-sx)*(lineEndX-sx) + (lineEndY-sy)*(lineEndY-sy) );
					
					
                    console.log("------>"+ star2sun +" + "+ star2edge +" -> " + lineLength );
                    var half = (lineLength+star2edge+star2sun)/2 ;
					half = half*(half-lineLength)*(half-star2edge)*(half-star2sun);
                    var fieldHeron = ( half > 0 ) ? Math.sqrt( half ) : false ;
                    var fieldStraight = lineLength * star.size/4 ;
                    console.log( fieldStraight +' >= ' + fieldHeron );*/
                   // console.log( "LINE["+lineEndX + ","+lineEndY+"] | STAR["+sx +","+sy +"]" );
                  //  console.log( "CANVAS:["+ this.canvas.width +","+ this.canvas.height +"]" );
                   
                    
					// if( !closestStarDistance || Math.sqrt( star.posX*star.posX + star.posY*star.posY ) < closestStarDistance )
					// {
						// alert( star.id );
					// }
                }
            }
	}

//draws all elements on the screen
	this.draw = function()
	{	
            var c= this.context;
            /**if( !this.loop )
            {
                    c.fillStyle="#000";
                    c.fillRect(0,0,this.canvas.width,this.canvas.height);
                    c.fill();
                    c.fillStyle="#FFF";
                    c.font="20px Courier";
                    c.textAlign="center";
                    c.fillText("PAUZA",this.canvas.width/2,this.canvas.height/2,this.canvas.width);
                    c.fillStyle="#000";
                    return false;
            }*/
          /*  if(!this.loop)
            { 
			//c.save();
			//c.translate( this.canvas.width/2,this.canvas.height/2 );
                var planet = this.planets[0];
                var star = this.stars[0];
				var sx = star.posX;
				var sy = star.posY;
				var lineLength = Math.sqrt( ( canvas.width/2*canvas.width/2 ) + ( canvas.height/2*canvas.height/2 ) );
				var lineEndX = lineLength * Math.sin( planet.angle ) + this.canvas.width/2;
				var lineEndY = lineLength * Math.cos( planet.angle ) + this.canvas.height/2;
				//console.log( planet.angle );
                c.beginPath();
                c.strokeStyle="#FFF";
                c.moveTo( this.canvas.width/2,this.canvas.height/2 );
                c.lineTo( sx,sy );
                c.lineTo( lineEndX,lineEndY );
                c.lineTo( this.canvas.width/2,this.canvas.height/2 );
                c.stroke();
				//c.restore();
                return false;
            }*/
            c.fillRect(0,0,canvas.width,canvas.height);
            c.save();
            c.translate( this.canvas.width/2,this.canvas.height/2 );
            c.fillStyle="#000";
            c.strokeStyle="#FFF";
            c.lineWidth="3";
            c.beginPath();
            c.arc(0,0,.3,0,Math.PI*2);
            c.stroke();

            for( i in this.planets )
            {
                this.planets[i].draw(c,this.loop);
            }
            c.restore();

            for( var i in this.stars)
            {
                this.stars[i].draw(c);
            }
		
	}
//the gam loop
	this.gameLoop = function()
	{
		
		for( i in this.planets )
		{
			var planet = this.planets[i];
			planet.angle += (planet.directory) ? planet.speed : planet.speed*(-1) ;
			if( Math.abs(planet.angle) > Math.PI*2 )
			{
				planet.speed = Math.floor( Math.random()*7+5 )/(100+30*i) ;
				planet.angle = 0 ;
				//if( Math.floor( Math.random()*100+1 ) <= 10 )planet.directory = !planet.directory;
			}
		}
                
                for( i in this.stars )
                {
                    var star = this.stars[i];
                    star.directionAngle = star.directionAngle%360;
                    var speed = 1;
                    star.posX += Math.sin(star.directionAngle)*speed;
                    star.posY += Math.cos(star.directionAngle)*speed;
                    //if( Math.random()*100 <= 2 )star.directionAngle ++;
//the following code prevents stars going into the planet system(they dissapear) or out of the area(they're supposed to turn back)           
                    if( star.posX+star.size/2 > this.canvas.width )
                    {
                        star.posX = this.canvas.width-star.size/2;
                        star.directionAngle -= 2*(star.directionAngle - Math.PI);
                    }
                    else if( star.posX-star.size/2 < 0 )
                    {
                        star.posX = star.size/2;
                        star.directionAngle += 2*(Math.PI - star.directionAngle);
                    }
                    else if( star.posY+star.size/2 > this.canvas.height )
                    {
                        star.posY = this.canvas.height-star.size/2;
                        star.directionAngle -= 2*(star.directionAngle - Math.PI/2*3);
                    }
                    else if( star.posY-star.size/2 < 0 )
                    {
                        star.posY = star.size/2;
                        star.directionAngle += 2*(Math.PI/2*3 - star.directionAngle);
                    }
                    else
                    {
                        var x = star.posX - this.canvas.width/2;
                        var y = star.posY - this.canvas.height/2;
                        var distFromMidle = Math.sqrt( (x*x) + (y*y) );
                        var orbitSize = 7+this.planets.length*6;
                        if( distFromMidle < orbitSize+star.size/2 )
                        {
                            star.visible=false;
                            //star.directionAngle += Math.PI ; //2*(Math.PI - star.directionAngle)
                        }
                        else
                            star.visible=true;
                    }
                }
		
		this.draw();
		
		if( this.loop )requestAnimationFrame( this.gameLoop.bind(this) );
	}
	
//adds a planet to the system
	this.addPlanet= function()
	{
		var size = Math.random()*1.5+2;
		var speed = Math.floor( Math.random()*7+5 )/(100+30*this.planets.length) ;
		var directory = Math.floor( Math.random()*2 ) ;
		var color = this.colors[ this.planets.length % this.colors.length ] ;
		this.planets[ this.planets.length ]= new Planet( size, speed, directory, color, this.planets.length );
	}
        
//adds a new star
        this.addStar = function()
        {
            var id = (!this.stars.length) ? 0 : parseInt(this.stars[ this.stars.length-1 ].id)+1 ;
            var color = this.colors[ Math.floor(Math.random()*this.planets.length) ] ;
            var size = Math.floor( Math.random()*3+15 );
//this algorithm prevents setting the star in the planet system(it has to be outside) or out of the area
            var planetSystemSize = 7+this.planets.length*6;
            //var lineLength = Math.floor( Math.random()*(this.canvas.height/2-planetSystemSize/2-size*4)) + planetSystemSize+size/2 ;
            var lineLength = Math.sqrt( (this.canvas.width/4+size/2)*(this.canvas.width/4+size/2)  + ( this.canvas.height/4+size/2)*(this.canvas.height/4+size/2 ) );
            var angle = Math.random()*Math.PI*2;
            var x = lineLength*Math.sin( angle ) + this.canvas.width/2 ;
            var y = lineLength*Math.cos( angle ) + this.canvas.height/2 ;
            //angle=Math.PI-angle;
            
            this.stars[ this.stars.length ] = new Star(
                                            id,
                                            color,
                                            0,
                                            size,
                                            x,
                                            y,
                                            Math.random()*Math.PI*2);
        }
        
        
        
//////////////////////////////////////////////////////
//                  HELPER FUNCTIONS                //
//////////////////////////////////////////////////////
//copies sign(+/-) from a given variable(arg0) and returns the variable(arg1) modified
    this.copySign = function( from , to )
    {
        if( from < 0 )
        {
            if( to < 0 )return to;
            else return -to;
        }
        else
        {
            if( from > 0 )return to;
            else return -to;
        }
    }
	
	this.compareSign = function( a , b )
	{
		if( a < 0 && b >= 0 )return false;
		if( a >= 0 && b < 0 )return false;
		return true;
	}
    
}